#include "addoperationwidget.h"
#include "choosefrienddialog.h"
#include "addfrienddialog.h"
#include <QGraphicsDropShadowEffect>
#include <QToolButton>
#include <QPainter>
#include <QPainterPath>
#include "model/data.h"


/* ------------------------------- 快捷窗口 ------------------------------- */
AddOperationWidget::AddOperationWidget(QWidget *parent) : QWidget{parent}
{
    // 1. 设置基本效果
    this->setAttribute(Qt::WA_TranslucentBackground);
    this->setWindowFlags(Qt::FramelessWindowHint | Qt::Popup | Qt::NoDropShadowWindowHint);
    this->setFixedSize(150, 170);
    this->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    // 2. 中央容器
    QWidget* centralWidget = new QWidget(this);
    centralWidget->setObjectName("OperationWidget");
    centralWidget->setStyleSheet(
        "#OperationWidget {"
        "   background-color: white;"
        "   border-radius: 8px;"
        "}"
        );

    // 阴影效果
    QGraphicsDropShadowEffect* effect = new QGraphicsDropShadowEffect();
    effect->setOffset(0, 3);
    effect->setColor(QColor(68, 68, 68, 150));
    effect->setBlurRadius(8);
    centralWidget->setGraphicsEffect(effect);

    // 3. 主布局 - 添加内边距为箭头和阴影留出空间
    QVBoxLayout* mainLayout = new QVBoxLayout(this);
    mainLayout->setContentsMargins(10, 15, 10, 10);
    mainLayout->addWidget(centralWidget);

    // 4. 内容布局
    layout = new QVBoxLayout(centralWidget);
    layout->setSpacing(8);
    layout->setContentsMargins(5, 5, 5, 5);

    // 5. 按钮构造
    createBtn(QIcon(":/resource/image/createGroup.png"), "发起群聊");
    createBtn(QIcon(":/resource/image/addFriend.png"), "添加朋友");
    createBtn(QIcon(":/resource/image/note.png"), "新建笔记");

    // 6. 查找 CustomToolButton
    for (auto btn : findChildren<CustomToolButton*>()) {
        if (btn->text().contains("群聊")) { // CustomToolButton 提供 text 方法来获取文本
            connect(btn, &CustomToolButton::clicked, this, [=]() { // <--- 连接 CustomToolButton 的 clicked 信号
                UserInfo userInfo; // 确保 UserInfo 类可用
                ChooseFriendDialog* chooseFriendDialog = new ChooseFriendDialog(this, userInfo.userId, "发起群聊");
                chooseFriendDialog->exec();
                this->close();
            });
        } else if (btn->text().contains("朋友")) {
            connect(btn, &CustomToolButton::clicked, this, [=]() {
                AddFriendDialog* addFriendDialog = new AddFriendDialog(this);
                addFriendDialog->exec();
                this->close();
            });
        } else if (btn->text().contains("笔记")) {
            connect(btn, &CustomToolButton::clicked, this, [=]() {

                this->close();
            });
        }
    }
}

void AddOperationWidget::paintEvent(QPaintEvent *event){
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);

    // 绘制箭头 (位于窗口顶部中央)
    QPainterPath arrowPath;

    // 绘制顶部箭头 (三角形)
    const int arrowWidth = 12;
    const int arrowHeight = 8;
    const int arrowX = width() / 2 - arrowWidth / 2;
    const int arrowY = 15 - arrowHeight; // 稍微靠下一点

    arrowPath.moveTo(arrowX, arrowY + arrowHeight);
    arrowPath.lineTo(arrowX + arrowWidth / 2, arrowY);
    arrowPath.lineTo(arrowX + arrowWidth, arrowY + arrowHeight);
    arrowPath.closeSubpath();

    painter.fillPath(arrowPath, Qt::white);

    // 调用基类绘制其他内容
    QWidget::paintEvent(event);
}

void AddOperationWidget::createBtn(const QIcon &avatar, const QString &text){
    // 使用自定义的 CustomToolButton
    CustomToolButton *btn = new CustomToolButton(avatar, text);
    layout->addWidget(btn);
}

/* ------------------------------- 按钮元素 ------------------------------- */
CustomToolButton::CustomToolButton(const QIcon &icon, const QString &text)
    : curText(text)
{
    this->setFixedSize(120, 40);
    this->setCursor(Qt::PointingHandCursor);
    this->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    this->setAttribute(Qt::WA_TranslucentBackground, false); // 设置背景透明，以便 paintEvent 绘制自定义背景

    layout = new QHBoxLayout(this);
    layout->setContentsMargins(10, 0, 10, 0); // 整体左右内边距
    layout->setSpacing(8); // 图标和文字之间的精确间距！

    QLabel* iconLabel = new QLabel(this);
    iconLabel->setPixmap(icon.pixmap(QSize(20, 20)));
    iconLabel->setFixedSize(QSize(20, 20));

    QLabel* textLabel = new QLabel(text);
    textLabel->setStyleSheet("font-size: 14px; font-family: 'Microsoft YaHei', 'Segoe UI', sans-serif; color: #333;");

    layout->addWidget(iconLabel);
    layout->addWidget(textLabel, 1); // 文本占据剩余空间
}

void CustomToolButton::mousePressEvent(QMouseEvent *event) {
    if (event->button() == Qt::LeftButton) {
        emit clicked(); // 发射点击信号
    }
    QWidget::mousePressEvent(event);
}

void CustomToolButton::enterEvent(QEnterEvent *event) {
    isHovered = true;
    update(); // 重新绘制以显示悬停效果
    QWidget::enterEvent(event);
}

void CustomToolButton::leaveEvent(QEvent *event) {
    isHovered = false;
    update(); // 重新绘制以移除悬停效果
    QWidget::leaveEvent(event);
}

void CustomToolButton::paintEvent(QPaintEvent *event) {
    Q_UNUSED(event);
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing); // 抗锯齿

    // 绘制背景
    QColor backgroundColor = Qt::transparent; // 默认透明
    if (isHovered) {
        backgroundColor = QColor(7, 193, 96); // 悬停时的背景色
    }

    if (backgroundColor.isValid()) {
        painter.setBrush(backgroundColor);
        painter.setPen(Qt::NoPen);
        painter.drawRoundedRect(rect(), 8, 8); // 绘制圆角矩形背景
    }

    // 调用基类的 paintEvent 确保子控件被绘制
    QWidget::paintEvent(event);
}

QString CustomToolButton::text() const {
    return curText; // 返回存储的文本
}

















